package com.ruoyi.web.controller.miMall;

import java.util.List;
import java.util.Objects;

import com.bye.xiaomi_mall.domain.*;
import com.bye.xiaomi_mall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.domain.AjaxResult;

import static com.ruoyi.Util.TokenUtil.decrypt;
import static com.ruoyi.Util.TokenUtil.encrypt;

@RestController
@RequestMapping("/miUser")
public class MiUserController {
    @Autowired
    private ITdGoodsCategoryService tdGoodsCategoryService;
    @Autowired
    private ITdUserService userService;
    @Autowired
    private ITdGoodsService tdGoodsService;
    @Autowired
    private ITdShoppingCartService tdShoppingCartService;

    //    登录验证
    @RequestMapping("/login")
    public AjaxResult login(TdUser tdUser) throws Exception {

        TdUser userInfo = userService.selectTdUserByPhone(tdUser.getPhone());
        if (null == userInfo) {
            return AjaxResult.error("没有该用户");
        }
        if (!Objects.equals(userInfo.getPassword(), tdUser.getPassword())) {
            return AjaxResult.error("密码错误");
        }
        // 加密
        String miUserToken = encrypt(String.valueOf(userInfo.getId()));

        // 解密
        String decryptedString = decrypt(miUserToken);

        userInfo.setId(null);
        userInfo.setPassword(null);
        return AjaxResult.success("登录成功").put("miUserToken", miUserToken).put("userInfo", userInfo);
    }

    //    注册
    @RequestMapping("/addUser")
    public AjaxResult addUser(@RequestBody TdUser tdUser){

        TdUser userInfo = userService.selectTdUserByPhone(tdUser.getPhone());
        if (null != userInfo) {
            return AjaxResult.error("当前用户已存在");
        }
        userService.insertTdUser(tdUser);
        return AjaxResult.success("注册成功");
    }

    //查看购物车
    @RequestMapping("/selectShoppingCart")
    public AjaxResult selectShoppingCart(@RequestHeader("miUserToken") String token) throws Exception {
        String userId = decrypt(token);
        List<TdShoppingCart> shoppingCartList = userService.selectShoppingCart(userId);
        return AjaxResult.success().put("shoppingCartList", shoppingCartList);
    }

    //添加到购物车
    @RequestMapping("/addShoppingCart")
    public AjaxResult addShoppingCart(@RequestBody TdShoppingCart cat) throws Exception {
        cat.setUserId(Long.valueOf(decrypt(cat.getMiUserToken())));
        tdShoppingCartService.insertTdShoppingCart(cat);
        return AjaxResult.success();
    }

    //删除购物车
    @RequestMapping("/deleteShoppingCart/{id}")
    public AjaxResult deleteShoppingCart(@PathVariable("id") Long id) {
        tdShoppingCartService.deleteTdShoppingCartById(id);
        return AjaxResult.success();
    }

    //查看收货地址
    @RequestMapping("/selectAddress")
    public AjaxResult selectAddress(@RequestHeader("miUserToken") String token) throws Exception {
        String userId = decrypt(token);
        List<TdAddress> addressList = userService.selectAddress(userId);
        return AjaxResult.success().put("addressList", addressList);

    }






}